package springjdbc;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * 账户操作实现类
 */
@Repository
public class AccountDaoImpl implements AccountDao{
    //声明JdbcTemplate属性及其setter方法
    @Resource
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate){
        this.jdbcTemplate = jdbcTemplate;
    }
    //添加账户
    public int addAccount(Account account){
        //定义sql语句
        String sql = "insert into account(username,balance) values(? , ?)";
        //定义数组来存储sql语句中的参数
        Object[] obj = new Object[]{
                account.getUsername(),
                account.getBalance()
        };
        //执行添加操作，返回的是受sql语句影响的记录条数
        int num = this.jdbcTemplate.update(sql,obj);
        if (num > 0){
            System.out.println("插入成功");
        }
        else {
            System.out.println("插入失败");
        }
        return num;
    }
    //更新账户
    public int updateAccount(Account account){
        //定义sql
        String sql = "update account set username = ? , balance = ? , where id = ?";
        //定义数组来存储sql语句中的参数
        Object[] params = new Object[]{
                account.getUsername(),
                account.getBalance(),
                account.getId()
        };
        //执行更新操作，返回的是受sql语句影响的记录条数
        int num = this.jdbcTemplate.update(sql,params);
        return num;
    }

    @Override
    public int deleteAccount(int id) {
        return 0;
    }

    @Override
    public Account findAccountById(int id) {
        return null;
    }

    //删除账户
    public int deleteAccount(Integer id){
        //定义sql
        String sql = "delete from account where id = ?";
        //执行删除操作
        int num = this.jdbcTemplate.update(sql,id);
        return num;
    }

    //通过id查询账户数据信息
    public Account findAccountById(Integer id){
        //定义sql语句
        String sql = "select * from account where id = ?";
        //创建一个新的BeanPropertyRowMapper对象
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
        //将id绑定到sql语句中，并通过RowMapper返回一个Object类型的单行记录
        return this.jdbcTemplate.queryForObject(sql,rowMapper,id);
    }
    //查询所有账户信息
    public List<Account> findAllAccount(){
        //定义sql语句
        String sql = "select * from account";
        //创建一个新的BeanPropertyRowMapper对象
        RowMapper<Account> rowMapper = new BeanPropertyRowMapper<Account>(Account.class);
        //执行静态SQL查询，并通过RowMapper返回结果
        return this.jdbcTemplate.query(sql,rowMapper);
    }

    @Override
    public void transfer(String outUser, String inUser, Double money) {
        //汇款操作1,从汇款人账户划走钱
        String sql = "update account set balance=balance-? where username=?";
        Object[] args = new Object[]{
                money,
                outUser
        };
        this.jdbcTemplate.update(sql,args);
        //汇款操作2,收款人账户接收钱
        String sql1 = "update account set balance=balance+? where username=?";
        Object[] args1 = new Object[]{
                money,
                inUser
        };
        this.jdbcTemplate.update(sql,args);


    }

}
